package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.VideoVo;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.lang.invoke.VolatileCallSite;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 根据用户的id 查询 推荐的小视频
     */
    @Override
    public PageBeanVo findVideoByUserId(Integer pageNumber, Integer pageSize, Long loginUserId) {

        //1.查询推荐的视频
        int startIndex = (pageNumber -1 ) * pageSize;
        Query query = new Query(
                Criteria.where("userId").is(loginUserId) //给谁推送
        ).with(Sort.by( Sort.Order.desc("score") ,Sort.Order.desc("date")  ))//排序
        .skip(startIndex).limit(pageSize);//查询数据
        List<RecommendVideo> recommendVideos = mongoTemplate.find(query, RecommendVideo.class);

        //如果没有找到推荐 - 自己构建几条假数据


        List<Video> videoList = new ArrayList<>();
        //2.根据推荐的视频id 查询视频的详情信息
        if(CollectionUtil.isNotEmpty(recommendVideos)){
            for (RecommendVideo recommendVideo : recommendVideos) {
                //查询video对象
                Video video = mongoTemplate.findById(recommendVideo.getVideoId(), Video.class);
                videoList.add(video);
            }
        }

        long counts = mongoTemplate.count(query, RecommendVideo.class);
        return new PageBeanVo(pageNumber , pageSize, counts , videoList);
    }

    @Autowired
    private IdService idService;
    /**
     * 保存视频
     * @param video
     */
    @Override
    public void saveVideo(Video video) {
        //1.保存到视频表
        video.setCreated(System.currentTimeMillis());//时间
        video.setVid(idService.getNextId(ConstantUtil.VIDEO_ID)); //生成大数据使用id数据
        mongoTemplate.save(video);
        //2.保存到推荐表 -> 可要可不要
        if(video!=null){
            RecommendVideo recommendVideo  = new RecommendVideo();
            recommendVideo.setDate(System.currentTimeMillis());
            recommendVideo.setVid( video.getVid() );
            recommendVideo.setUserId( video.getUserId() );
            recommendVideo.setVideoId( video.getId() );
            recommendVideo.setScore(RandomUtil.randomDouble(60,90));
            mongoTemplate.save(recommendVideo);
        }

    }

    /**
     * 分页查询用户的视频
     * @param pageNumber
     * @param pageSize
     * @param uid
     * @return
     */
    @Override
    public PageBeanVo findVideoByUid(Integer pageNumber, Integer pageSize, Long uid) {
        //1.查询推荐的视频
        int startIndex = (pageNumber -1 ) * pageSize;
        Query query = new Query(
                Criteria.where("userId").is(uid) // 查询我的视频
        ).with(Sort.by( Sort.Order.desc("date")  ))//排序
                .skip(startIndex).limit(pageSize);//查询数据
        List<Video> videoList = mongoTemplate.find(query, Video.class);

        //查询总记录数
        long counts = mongoTemplate.count(query, Video.class);
        return new PageBeanVo(pageNumber , pageSize, counts , videoList);
    }
}
